<?php

namespace App\Http\Controllers;

use App\Models\Report;
use App\Models\Setting;
use App\Models\Staff;
use App\Repositories\WxMapRepository;
use Illuminate\Database\Eloquent\Builder;

class TestController extends Controller
{
    public function t1()
    {
        dd(Setting::query()->where('mall_id', 0)->first());
//        return Report::query()->where('mall_id',1)
//            ->with([
//                'wxuser'=>function($query){
//                    $query->select(['wx_nickname','id','wx_headimgurl']);
//                }
//            ])
//            ->paginate()->toArray();

//        $this->t2();
//        dd(1);
        $message = [
            'FromUserName' => 'openid:65fe8fc1103b7',
            'EventKey' => 'qrcode:'.request()->get('key'),
        ];
        $mall_id = 1;
//
//        $report = Report::query()->where('wx_ticket', $message['EventKey'])->orderBy('id','desc')->first();
//        if(empty($report)){
//            return null;
//        }
//        $url_data = [
//            'title'       => '查看报告',
//            'description' => '查看检查报告完整信息',
//            'url'         => route('wx.auth',$mall_id).'?scene='.WxMapRepository::FOOT_WORK_STR,
//            'image'       => url('/images/kj.png'),
//        ];
//        if(empty($report->wx_openid)){
//            $report->wx_openid = $message['FromUserName'];
//            $report->save();
//        }
//        dd(2);

        $staff = Staff::firstOrNew(['wx_openid' => $message['FromUserName']]);
        if(empty($staff->wx_nickname)){
            $url_data = [
                'title'       => '完善登陆信息',
                'description' => '请点击完善登陆信息',
                'url'         => route('wx.auth',$mall_id).'?scene='.WxMapRepository::PC_LOGIN_STR,
                'image'       => url('/images/kj.png'),
            ];
        }
//        dump($url_data);
        $staff->mall_id = $mall_id;
        $staff->login_key = $message['EventKey'];
        $staff->save();
        dump($staff->toArray());
    }

    public function t2()
    {

        $re = new WxMapRepository();
        $user = $re->saveWxUser([
            'id' => 'openid:65fe8fc1103b7',
            'unionid' => 'unionid:'.uniqid(),
            'nickname' => 'nick_name:'.uniqid(),
            'headimgurl' => 'nick_name:'.uniqid(),
            'sex' => 0,
        ], 1);
//        dd($user);
        $re->syncWxUserInfoToReport($user);
    }
}
